package au.edu.jcu.it.numberguess;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CustomViewPager extends ViewPager {
	float lastX = 0;
	boolean onFreePlaySetting = false;
	boolean lockScroll = false;
	protected boolean onSwipeOutAtEnd;
	protected OnSwipeOutListener mListener;

	public interface OnSwipeOutListener {
		public void onSwipeOutAtEnd();
	}

	public void setOnSwipeOutListener(OnSwipeOutListener listener) {
		mListener = listener;
	}

	public CustomViewPager(Context context) {
		super(context);

	}

	public CustomViewPager(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		if (onFreePlaySetting == false) {
			switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN:
				lastX = ev.getX();
				lockScroll = false;
				return super.onTouchEvent(ev);
			case MotionEvent.ACTION_MOVE:

				if (lastX > ev.getX()) {
					lockScroll = false;

					if (getCurrentItem() == getAdapter().getCount() - 1) {
						setOnSwipeOutAtEnd(true);
						mListener.onSwipeOutAtEnd();

					}

				} else {
					lockScroll = true;

				}

				lastX = ev.getX();
				break;
			}
		}

		lastX = ev.getX();

		if (lockScroll) {
			return false;
		} else {
			return super.onTouchEvent(ev);
		}

	}

	public void setOnFreePlaySetting(boolean b) {
		onFreePlaySetting = b;
	}

	public boolean getOnFreePlaySetting() {
		return onFreePlaySetting;
	}

	public void setOnSwipeOutAtEnd(boolean b) {
		onSwipeOutAtEnd = b;
	}

	public boolean getOnSwipeOutAtEnd() {
		return onSwipeOutAtEnd;
	}

}
